const path = require('path');
const os = require('os');
const rootPath = path.join(__dirname, './');
const webpack = require('webpack');
const theme = require('./package.json').theme;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const nodeModule = path.join(rootPath, 'node_modules');
const TerserPlugin = require('terser-webpack-plugin');
const threads = os.cpus().length; // cpu核数
const getStyleLoaders = preProcessor => {
  return [
    MiniCssExtractPlugin.loader,
    'css-loader',
    {
      loader: 'postcss-loader',
      options: {
        postcssOptions: {
          plugins: [
            'postcss-preset-env', // 能解决大多数样式兼容性问题
          ],
        },
      },
    },
    preProcessor,
  ].filter(Boolean);
};
module.exports = {
  entry: {
    entry: ['eventsource-polyfill', path.join(rootPath, 'src/entry.js')],
  },
  output: {
    path: path.join(rootPath, 'dist/dist'),
    filename: '[name]-[hash:6].js',
    chunkFilename: '[name].[hash:6].chunk.js',
    publicPath: '/dist/',
  },
  resolve: {
    extensions: ['', '.js', '.jsx', '.less','scss'],
    // 路径别名
    alias: {
      VIEW: path.join(rootPath, 'src/view'),
      ASSET: path.join(rootPath, 'src/asset'),
      COMPONENT: path.join(rootPath, 'src/component'),
      HOOKS: path.join(rootPath, 'src/hooks'),
      ROUTE: path.join(rootPath, 'src/route'),
      UTIL: path.join(rootPath, 'src/util'),
      SERVICE: path.join(rootPath, 'src/service'),
      STORE: path.join(rootPath, 'src/store'),
      MODEL: path.join(rootPath, 'src/model'),
      NODE: path.join(nodeModule, ''),
    },
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'thread-loader', // 开启多进程
            options: {
              workers: threads, // 数量
            },
          },
          {
            loader: 'babel-loader',
            options: {
              cacheDirectory: true, // 开启babel编译缓存
              cacheCompression: false, // 缓存文件不要压缩
              plugins: ['@babel/plugin-transform-runtime'], // 减少代码体积
            },
          },
        ],
      },
      {
        test: /(fontawesome-webfont|glyphicons-halflings-regular)\.(woff|woff2|ttf|eot|svg)($|\?)/,
        use: ['url-loader'],
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        use: ['url-loader'],
      },
      {
        test: /\.json5$/, //.json5文件兼容性更好   & webpack >= v2.0.0 默认支持导入 JSON 文件
        use: ['json5-loader'],
        exclude: /node_modules/,
      },
      {
        test: /\.html$/,
        use: 'html-loader',
      },
      {
        test: /\.less$/, //antd演示引入
        use: [
          MiniCssExtractPlugin.loader,
          { loader: 'css-loader' },
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  'postcss-preset-env', // 能解决大多数样式兼容性问题
                ],
              },
            },
          },
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true,
                modifyVars: theme, //antd 配置
              },
            },
          },
        ],
        include: /node_modules/,
      },
      {
        test: /\.less$/,
        use: getStyleLoaders('less-loader'),
        exclude: /node_modules/,
      },
      {
        test: /\.s[ac]ss$/,
        use: getStyleLoaders('sass-loader'),
      },
      {
        test: /\.css$/,
        use: getStyleLoaders(),
        exclude: /node_modules/,
      },
    ],
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV),
        port: JSON.stringify(process.env.port),
      },
      __DEV__: process.env.NODE_ENV === 'development',
      __COMPONENT_DEVTOOLS__: false,
      __ENV__: JSON.stringify(process.env.NODE_ENV),
    }),
    new CopyWebpackPlugin([
      {
        from: path.join(rootPath, 'static'),
        to: path.join(rootPath, 'dist/static'),
      },
    ]),
  ],
  optimization: {
    minimizer: [
      new TerserPlugin({
        parallel: threads, // 开启多进程
      }),
    ],
  },
};
